#!/bin/bash
#1，安装git工具
#2，下载、安装

#nginx
curl -o nginx-1.16_init.sh https://gitee.com/ffing/onekey/raw/master/nginx-1.16_init.sh && bash nginx-1.16_init.sh
#aria2
curl -o aria2-1.34_init.sh https://gitee.com/ffing/onekey/raw/master/aria2-1.34_init.sh && bash aria2-1.34_init.sh
#yaaw
#####需求替换
TOOLS="git"
SOFT="yaaw"
VER="zh"
SER="nginx"
SER1="aria2"
PORT="6808"
IPLAN=$(hostname -I | awk -F " " '{printf $1}')
IPWAN=$(curl http://pv.sohu.com/cityjson 2>> /dev/null | awk -F '"' '{print $4}')



#####无需替换
GITEXT=".git"
FILEEXT="_init.sh"
CONFEXT=".conf"

TAG="."
TAG1=".*"
NUM="[0-9]"
RPMNAME=""

SHELLDIR="/opt/shell/"
SOFTDIR="/opt/"$SOFT"-"$VER"/"

GITADDR="https://gitee.com/ffing/"
GITDOWN=""$GITADDR""$SOFT"-"$VER""$GITEXT""

NGINXDIR="/usr/share/nginx/"
NGINXCONFDIR="/etc/nginx/conf.d/"

STADIR=""$NGINXDIR""$SOFT"-"$VER""
SOFTCONFNAME=""$SOFT"-"$VER""$CONFEXT""

SOFTDOWNCONFFILE=""$SOFTDIR""$SOFTCONFNAME""
SOFTSTACONFFILE=""$NGINXCONFDIR""$SOFTCONFNAME""

#####安装TOOLS类
pName=$(rpm -qa | grep "^${TOOLS}${TAG}${NUM}${TAG}${NUM}")
if [ $? -eq 0 ]
then
       echo -e	"----------已存在，无需安装----------\n"${pName}""
	   echo
else
echo "----------正在安装 "${TOOLS}"……"
yum -y install $TOOLS
               if [ $? -eq 0 ]
               then
                        echo "----------安装完成 "${TOOLS}""
						echo
               else
                        echo -e "\e[41m >>>错误>>>安装失败 "${TOOLS}"\e[0m"
						echo
               fi
fi


#下载站点文件
if [ ! -d $SOFTDIR ];then
echo "创建$SOFT下载目录 ${SOFTDIR}"
git clone $GITDOWN $SOFTDIR 
else
echo "存在$SOFT下载目录 ${SOFTDIR}"
fi

#复制到nginx根目录
if [ ! -d $STADIR ];then
echo "复制$SOFT站点目录 $STADIR"
cp -r $SOFTDIR $STADIR 
else
echo "存在$SOFT站点目录 $STADIR"
fi

#复制配置文件
if [ ! -f $SOFTSTACONFFILE ];then
echo "复制站点配置文件$SOFTSTACONFFILE"
\cp $SOFTDOWNCONFFILE $SOFTSTACONFFILE
else
echo "存在站在配置文件$SOFTSTACONFFILE"
fi

#####开放防火墙端口
echo "配置永久开放端口：$PORT"
sed -i "s/\/zone/port protocol\=\"tcp\" port\=\"${PORT}\"\//g" /etc/firewalld/zones/public.xml
echo "</zone>" >>/etc/firewalld/zones/public.xml
firewall-cmd --reload  > /dev/null 2>&1

#####关闭seliunx
#临时关闭SElinux
setenforce 0
#sed修改配置文件，实现永久禁用selinux
sed -i "s/SELINUX=enforcing/SELINUX=disable/g" /etc/selinux/config



#####重启服务
#kill -9 $(ps -ef|grep ${SER} |gawk '$0 !~/grep/ {print $2}' |tr -s '\n' ' ') >/dev/null 2>&1
systemctl restart $SER
systemctl restart $SER1

#####服务状态
ps -ef|grep $SER|grep -vE "grep|$$" >/dev/null
if [ $? -eq 0 ];then
        echo -e "\e[42m "${SER}" 		已经启动\e[0m"
else
		echo -e "\e[41m "${SER}" 		启动失败\e[0m"
fi

ps -ef|grep $SER1|grep -vE "grep|$$" >/dev/null
if [ $? -eq 0 ];then
        echo -e "\e[42m "${SER1}" 		已经启动\e[0m"
else
		echo -e "\e[41m "${SER1}" 		启动失败\e[0m"
fi



echo
echo "—————————————————aria2+yaaw————————————————"
echo "aria2下载目录：/aria2_download/"
echo "aria2监听端口：6800"
echo "aria2配置目录：/etc/aria2/"
echo "aria2日志文件：/var/log/aria2.log"
echo "yaaw 内网访问：http://"$IPLAN":"$PORT""
echo "yaaw 外网访问：http://"$IPWAN":"$PORT""
echo "启动：systemctl start aria2"
echo "停止：systemctl stop aria2"
echo "重启：systemctl restart aria2"
echo -e "\e[33m注：已永久关闭SeLinux\e[0m"
echo "———————————————————————————————————————————"